package wili.attrMapping.core.del;

import wili.attrMapping._user.b1.AttConst;
import wili.attrMapping._user.b1.AttConst.Bugs;
import wili.attrMapping._user.b1.AttMapingRule_main;
import wili.attrMapping._user.b1.B1com;
import wili.attrMapping._user.b1.exp.AndOrExp2;
import wili.attrMapping._user.b1.exp.AndOrNode2;
import wili.attrMapping.core.UserConf;
import wili.attrMapping.core.ValueMapingNode;
import wili.c.Ca;
import wili.c.Misc9a;

/**
 * 
 * @author weilai2@163.com
 *
 */
class OldNewEditable extends OldNewAtts {
	OldEditable oldObj2;
	NewEditable newObj2;

	public OldNewEditable(OldEditable frombean, NewEditable toBean) {
		super(frombean, toBean);
		this.oldObj2 = frombean;
		this.newObj2 = toBean;
	}

	String newFmOf(String oldFm) {
		return oldObj2.formula.toString();
	}

	String ruleOf(String newFm, String newAtt) {
		return "";
	}

	@Override
	protected ValueMapingNode refreshTargetBean(ValueMapingNode ruleRoot, UserConf conf) {
		// 旧值 ~>新值, (新值，新属性值)~>规则值
		Ca.asert(false, "1206");
		String FORMULA2 = "@{formula}";
		String newFm = newFmOf(this.oldObj2.formula);
		String ruleSt = ruleOf(newFm, this.newObj2.formula);
		String newSt = newObj2.formula, newRuleSt = newSt;
		{
			// 概念：规则值，旧值，新值，新属性值；新值当是新属性值或其子集。
			// 旧值 ~>新值, (新值，新属性值)~>规则值
			// 若新值存在于新属性值中，可据此猜测出规则。eg. fm + `#a# && fm` ~> rule is `#a && @{fm}`
			String oldToSt = B1com.formularTransform(null, this.oldObj2.formula, false);
			AndOrNode2 newExp = AndOrExp2.newInst(-1, newObj2.formula).rootNode;
			newExp.sortSonsBySt(9);
			AndOrNode2 oldToExp = AndOrExp2.newInst(-1, oldToSt).rootNode;
			if (newExp.isAnd != null && newExp.isAnd) {// 表达式中可能局部出现多余括号，无法字串匹配
				int ind = AndOrNode2.indexOfExpNode(oldToExp, newExp.sons);
				if (ind != -1) {
					newExp.sons.set(ind, AndOrExp2.newInst(-1, FORMULA2).rootNode);
					newRuleSt = newExp.getString(1);
				}
			} else if (newExp.equals(oldToExp)) {
				newRuleSt = AndOrExp2.newInst(-1, FORMULA2).rootNode.getString(1);
			}
			newRuleSt = standValue(newSt);
		}
		{
			ValueMapingNode lastAttNod = ruleRoot.addOrMergeAtts(oldObj2, true);
			final ValueMapingNode ruleLeaf = lastAttNod.addOrMergeSon(oldObj.objName);// 取得规则叶
			String newFileName = this.newObj.file.file().getName();
			// Ca.log("ruleID=" + ruleLeaf.id);
			if (ruleLeaf.value == null || ruleLeaf.value.isEmpty()) {
				// 没有规则时，新规则就是规则
				String va = B1com.andExpToFormular(newRuleSt, ruleLeaf.value);
				ruleLeaf.setValue(va);
				ruleLeaf.addValue(va, newObj2.key, newFileName);
			} else {
				if (isEquals(ruleLeaf.value, newRuleSt)) {
					Ca.pause();
				} else {
					// 有规则时，若新旧规则不同，视为冲突
					/*
					 * 等价属性集aa共享1个规则叶f，aa[i]会和首值检测冲突。 10次冲突并不意味着有10种值，也许除首值外，其它值都相同。
					 */
					AttMapingRule_main.bugNum++;
					if (++errInc == -114)
						Ca.asert(ruleLeaf.value.contains("formula"), "todo");
					Ca.log("\n" + errInc + ",i" + ruleLeaf.id + "," + Bugs.msg(Bugs.InconsistentMapping) + ", "
							+ oldObj.file.file().getName() + " ~>" + newFileName);
					int size = ruleLeaf.oldAtts.size();
					String st2 = Misc9a.strOfMisc(ruleLeaf.oldAtts, -2, " ~>", ruleLeaf.newAtts, -2, //
							", \n\trule value:" + ruleLeaf.value);
					String st1 = Misc9a.strOfMisc(ruleLeaf.oldAtts, -1, " ~>", ruleLeaf.newAtts, -1, //
							", \n\trule value:" + newObj2.updateValue);
					String id2 = ruleLeaf.oldAtts.get(size - 2).key;
					String id1 = ruleLeaf.oldAtts.get(size - 1).key;// //findAttFromAncestor("guid", null).stringValue;
					Ca.log(" " + st2 + "\n " + st1);
					String st = " (`" + id2 + "`|`" + id1 + "`)";
					Ca.log(st.replace('`', '"'));
					// System.exit(0);
					ruleLeaf.addValue(newSt, newObj2.key, newFileName);
				}
			}
			{// 某fromId 未找到对应的toID, 但当前规则可能已经有值，仅pop此id.
			 // lastAttNod.oldAtts.pop();//
			}
			AttMapingRule_main.scanNum++;
			// newObj.updateValue(ruleLeaf.value);
			ruleRoot.print(-1);
			return lastAttNod;
		}
	}

	static boolean isEquals(String aa, String bb) {
		return aa.replaceAll("\\s+", "").equals(bb.replaceAll("\\s+", ""));
	}

	static int errInc;

	static String standValue(String val) {
		val = val.trim();
		if (val.matches("\\w+"))
			return val;
		String st = AttConst.placeholderOf("formula");
		val = val.replace(st, "(" + st + ")");
		if (!val.startsWith("{="))
			val = "{=" + val + "}";
		return val;
	}
}
